package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * SpringMvc处理全局异常
 * 需要使用注解*/
@ControllerAdvice(annotations = {Controller.class, RestController.class})
@ResponseBody//该返回类型数据全部都会转换成json字符串
@Slf4j
public class GlobalExceptionHandler {
    //代表了会处理所有的异常
    @ExceptionHandler(value = Exception.class)
    public R<String> handlerException(Exception e){
        log.info("-----异常处理-----");
        e.printStackTrace();
        return R.error("服务器异常："+e.getMessage());
    }
}
